package com.spring.demo.web.validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.spring.demo.core.entity.Ping;

/**
 * ping 验证
 * 
 * @author sean
 * 
 */
@Component
public class PingValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
		return Ping.class.equals(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "age", "valid.age");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "valid.name");

		Ping ping = (Ping) target;
		Integer age = ping.getAge();
		if (age != null && (age < 1 || age > 120)) {
			errors.rejectValue("age", "range.age");
		}
	}

}
